package com.liam.game.ui.bird;

import com.liam.game.util.Utils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

public class GamePanel extends JPanel {

    BufferedImage bg;

    Ground ground;

    Column column1;


    public GamePanel(GameFrame frame) {
        bg = Utils.getImg("static/img/bird/bg.png");

        ground = new Ground();

        column1 = new Column();

        // 鼠标监听
        MouseAdapter adapter = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
            }

            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                super.mouseExited(e);
            }

            @Override
            public void mouseMoved(MouseEvent e) {

            }
        };

        addMouseListener(adapter);
        addMouseMotionListener(adapter);

        // 键盘监听
        KeyAdapter keyAdapter = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                }
                repaint();
            }
        };
        // 加入到窗体的键盘监听器
        frame.addKeyListener(keyAdapter);

    }


    public void start(){

        while (true){
            ground.move();
            try {
                Thread.sleep(30);
                repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 专用画图方法
     *
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);

        g.drawImage(bg, 0, 0,null);

        g.drawImage(ground.img, ground.x , ground.y -23,null);
        g.drawImage(column1.img, column1.x , column1.y , column1.w, column1.h,null);

    }
}
